<?php

function global_resize($img, $thumbWidth, $newfilename, $new_path)
{ 

$imgInfo = getimagesize($img);

$name_array = explode(".", $newfilename);
			$file_part1		= $name_array[0];
			$file_part2		= $name_array[1];
			$newfileloc  = $new_path.$file_part1.$thumbWidth.".".$file_part2;
			$newfilename = $file_part1.$thumbWidth.".".$file_part2;
			
 switch ($imgInfo[2]) {

        case 1: $im = imagecreatefromgif($img); break;
        case 2: $im = imagecreatefromjpeg($img);  break;
        case 3: $im = imagecreatefrompng($img); break;
        default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
    }

$width = imagesx( $im );
$height = imagesy( $im );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

if($new_height > $height && $new_width > $width){$new_height = $height; $new_width = $width;}
      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresampled( $tmp_img, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      // save thumbnail into a file
      imagejpeg( $tmp_img, $newfileloc);
      
   return $newfileloc;   
 
   }   

?>